﻿// jQuery操作cookie的插件
// 用法:
// $.cookie('the_cookie'); // 取得cookie
// $.cookie('the_cookie', 'the_value'); // 设置cookie
// $.cookie('the_cookie', 'the_value', { expires: 7 }); // 设置cookie并设置过期时间，如果设置整数会帮你自动转换为天，其它时间请自行定义
// $.cookie('the_cookie', '', { expires: -1 });// 删除cookie  或者$.cookie('the_cookie', null); 也可以
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') {
        // 如果值和名字都有，那么就设置cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options);
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number')) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '; path=/';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};